package bg.uni_sofia.conf_manager.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@Entity
@NamedQueries({
		@NamedQuery(name = "checkExistLecturerName", query = "SELECT COUNT(l.username) FROM Lecturer l WHERE l.username = :username"),
		@NamedQuery(name = "checkExistLecturerMail", query = "SELECT COUNT(l.email) FROM Lecturer l WHERE l.email = :email"),
		@NamedQuery(name = "getLecturerById", query = "Select l FROM Lecturer l WHERE l.id = :id"),
		@NamedQuery(name = "getLecturerByName", query = "SELECT l FROM Lecturer l WHERE l.username = :username"),
		@NamedQuery(name = "updateLecturerPrivilege", query = "UPDATE Lecturer l SET l.isActive = :isActive WHERE l.username = :username"),
		@NamedQuery(name = "changePasswordByLecturerName", query = "UPDATE Lecturer l SET l.password = :password WHERE l.username = :username"),
		@NamedQuery(name = "changeEmailByLecturerName", query = "UPDATE Lecturer l SET l.email = :email WHERE l.username = :username"),
		@NamedQuery(name = "getAllConferencesByLecturer", query = "SELECT l.conferences FROM Lecturer l WHERE l.id = :id"),
		@NamedQuery(name = "getAllLecturers", query = "SELECT l FROM Lecturer l WHERE l.isActive = :isActive")})
@XmlRootElement
public class Lecturer implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String username;
	private String password;
	private String email;
	private String name;
	private boolean isActive;
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastLogin;
	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	private String biography;
	@OneToMany
	@XmlInverseReference(mappedBy = "lecturer")
	private List<Lecture> lectures;
	@ManyToMany
	private List<Conference> conferences;

	// private String photo;

	public Lecturer() {

	}

	public Lecturer(String username, String password, String email,
			String name, String biography, List<Lecture> lectures,
			List<Conference> conferences) {
		super();
		this.username = username;
		this.password = password;
		this.email = email;
		this.name = name;
		this.biography = biography;
		this.lectures = lectures;
		this.conferences = conferences;
		this.isActive = true;
		this.lastLogin = new Date();
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBiography() {
		return biography;
	}

	public void setBiography(String biography) {
		this.biography = biography;
	}

	public List<Lecture> getLectures() {
		return lectures;
	}

	public void setLectures(List<Lecture> lectures) {
		this.lectures = lectures;
	}
	
	@XmlTransient
	public List<Conference> getConferences() {
		return conferences;
	}

	public void setConferences(List<Conference> conferences) {
		this.conferences = conferences;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}
	
	public void addConference(Conference conference){
		getConferences().add(conference);
	}

}
